package com.csjy.migration.hnnd.business;


import com.csjy.migration.hnnd.*;
import com.csjy.migration.hnnd.annotation.BusinessDataConverter;

import javax.script.ScriptException;
import java.io.FileWriter;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 评审结论
 */
@BusinessDataConverter(
    insertSql =  "insert into nd_review_conclusion_table (ID, CREATE_NAME, CREATE_BY, CREATE_DATE, UPDATE_NAME, UPDATE_BY, UPDATE_DATE, SYS_ORG_CODE, SYS_COMPANY_CODE, BPM_STATUS, SERIAL_NUMBER, SOURCE_PROCESS, BUILD_TIME, REVIEW_CONCLUSION, PRO_ID, PROCESS_ID, FILE_ID)" +
    "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",

    updateSql = "UPDATE ND_REVIEW_CONCLUSION_TABLE\n" +
    "SET CREATE_NAME=?, CREATE_BY=?, CREATE_DATE=?, UPDATE_NAME=?, UPDATE_BY=?, UPDATE_DATE=?, SYS_ORG_CODE=?, SYS_COMPANY_CODE=?, BPM_STATUS=?, SERIAL_NUMBER=?, SOURCE_PROCESS=?, BUILD_TIME=?, REVIEW_CONCLUSION=?, PRO_ID=?, PROCESS_ID=?, FILE_ID=?\n" +
    "WHERE ID = ?\n",

    findMigratedIdListSql = "SELECT ID FROM ND_REVIEW_CONCLUSION_TABLE WHERE PRO_ID = ?",

    seq = 3


)
public class NdReviewConclusionTable extends AbstractBusiness {
  private static StrongUuidGenerator uuidGenerator = new StrongUuidGenerator();

  public NdReviewConclusionTable(DbUtil sourceDbUtil, DbUtil targetDbUtil, SourceBusinessData sourceData, ProgramArgument programArgument, String projectId) throws SQLException {
    super(sourceDbUtil, targetDbUtil, sourceData, programArgument, projectId);
  }


  @Override
  public void migrate() throws Exception {

    // 插入到新数据库
    int i = 1;
    for (Map wfAffixNotionObj : this.sourceData.getJwframeAuditConclusionList()) {
      try {
        String targetId = getCustomSourceIdWithoutFix(wfAffixNotionObj.get("PROCID").toString(), wfAffixNotionObj.get("TASKID").toString());
        int needMigratedResult = this.needMigrated(targetId);
        if (needMigratedResult == 0) {
          continue;
        }
        // 单条插入或更新
        if (needMigratedResult == 1) {
          this.targetDbUtil.updateByParams(updateSql, this.buildValues(wfAffixNotionObj, targetId,i), true);
          // 批量插入或更新 和 单条插入或更新 二选一
//             this.batchUpdateValues.add(this.buildValues(row));
        } else {
          this.targetDbUtil.updateByParams(insertSql, this.buildValues(wfAffixNotionObj, targetId,i));
          // 批量插入或更新 和 单条插入或更新 二选一
//             this.batchInsertValues.add(this.buildValues(row));
        }

      } catch (Exception e) {
        throw new Exception(String.format("评审结论表[%s]迁移失败:%s\n", wfAffixNotionObj.get("ITEMNO"), e.getMessage()));
      }

      i++;
    }
  }
    public List buildValues (Map wfAffixNotionObj, String id,int i) throws SQLException, ScriptException {
      // 准备参数
      List values = new ArrayList();
      // ID
      values.add(id);
      //CREATE_NAME	创建人名称
      values.add(Tool.removeOrgCodeFromEmpCode(wfAffixNotionObj.get("SUBMITEMPCODE").toString()));
      //CREATE_BY	创建人登录名称
      values.add(null);
      //CREATE_DATE	创建日期
      values.add((Timestamp)wfAffixNotionObj.get("CREATE_TIME"));
      //UPDATE_NAME	更新人名称
      values.add(null);
      //UPDATE_BY	更新人登录名称
      values.add(null);
      //UPDATE_DATE	更新日期
      values.add((Timestamp)wfAffixNotionObj.get("UPDATE_DATE"));
      //SYS_ORG_CODE	所属部门
      values.add(null);
      //SYS_COMPANY_CODE	所属公司
      values.add(null);
      //BPM_STATUS	流程状态
      values.add(1);
      //SERIAL_NUMBER	序号
      values.add(BigDecimal.valueOf(i));
      //SOURCE_PROCESS	来源流程
      values.add(wfAffixNotionObj.get("WFDISPLAYNAME"));
      //BUILD_TIME	生成时间
      values.add((Timestamp)wfAffixNotionObj.get("SUBMIT_DATE"));
      //REVIEW_CONCLUSION	评审结论
      String conclusion = Tool.decodeUnicode(wfAffixNotionObj.get("STRESTIMATE").toString()+","+Tool.decodeUnicode(wfAffixNotionObj.get("STRRESULT").toString()));
      values.add(conclusion);
      //PRO_ID	项目ID
      values.add(this.projectId);
      //PROCESS_ID	流程id
      values.add(wfAffixNotionObj.get("PROCID") + "-JW");
      //FILE_ID	评审结论ID
      Optional<Map> wfAffixNotionObjFileMap = this.sourceData.getWfAffixNotionObj().stream()
          .filter(wfAffixNotionObjFile -> {
            return (wfAffixNotionObjFile.get("WKFLAG").toString().equals("3")
                && wfAffixNotionObjFile.get("PROCID").toString().equals(wfAffixNotionObj.get("PROCID").toString())
                && wfAffixNotionObjFile.get("TASKID").toString().equals(wfAffixNotionObj.get("TASKID").toString()));
          })
          .findFirst();
      values.add(wfAffixNotionObjFileMap.isPresent() ? wfAffixNotionObjFileMap.get().get("INTMARK") : null);
      return  values;
    }

}
